// pages/growRoadEnroll/growRoadEnroll.js
let http = require('../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '成长之路',
    maskFlag: true,
    popupViewPay: true,
    popupViewEnroll: true,
    chooseNum: 0,
    areaIndex: 0,
    areaArray: [],
    areaChange: 0
  },
  // 地区picker
  areaChange(e) {
    let _this = this
    wx.setStorageSync('pickerid', e.detail.value)
    wx.setStorageSync('areaid', _this.data.areaArr[e.detail.value].id)
    console.log(e.detail.value)
    this.setData({
      areaIndex: e.detail.value,
      areaChange: 1
    })
  },
  // 跳转地图
  goMap(e) {
    let _this = this
    let lat = _this.data.lat
    let lng = _this.data.lng
    if (lat && lng) {
      wx.navigateTo({
        url: `/pages/map/map?lat=${lat}&lng=${lng}`,
      })
    }
  },
  // 校区选择
  bindPickerChange: function (e) {
    let school = this.data.schoolArr[e.detail.value].name
    console.log(this.data.schoolArr[e.detail.value].name)

    this.setData({
      schoolIndex: e.detail.value,
      school,
      chooseNum: 1
    })
    // 自动分班请求
    http.getHttp('/classes_lists', {
      school: school,
      course: wx.getStorageSync('project_id')
    }, {
      token: wx.getStorageSync('token')
    }).then(res => {
      console.log(res, '分班res')
      this.setData({
        classid:res.data.data.id  //？
      })
    })
  },
  // 监听名字
  nameEnroll(e) {
    let nameEnroll = e.detail.value
    this.setData({
      nameEnroll
    })
  },
  // 监听号码
  phoneEnroll(e) {
    let phoneEnroll = e.detail.value
    this.setData({
      phoneEnroll
    })
  },
  // 显示遮罩及打开自定义支付弹框
  showPayPopup(e) {
    let _this = this
    let nameEnroll = _this.data.nameEnroll
    let phoneEnroll = _this.data.phoneEnroll

    http.postHttp('/create_order', {
      project_id: wx.getStorageSync('project_id'),
      project_type: wx.getStorageSync('project_type'),
      name: nameEnroll,
      phone: phoneEnroll,
      scene: 2
    }, {
      token: wx.getStorageSync('token')
    }).then(res => {
      // 获取school_id
      http.getHttp('/user_info', {}, {
        token: wx.getStorageSync('token')
      }).then(res => {
        console.log(res, '个人信息')
        let school_id = res.data.data.grow.school_id
        console.log(school_id, 'school_id')
        
      })

      console.log(res, '订单response')
      // let payParam=Object.assign({
      // appId:res.data.data.appId,
      // nonceStr:res.data.data.nonceStr,
      // packageInfo:res.data.data.package,
      // paySign:res.data.data.paySign,
      // signType:res.data.data.signType,
      // timeStamp:res.data.data.timeStamp
      // })
      this.setData({
        // payParam,
        appId: res.data.data.appId,
        nonceStr: res.data.data.nonceStr,
        packageInfo: res.data.data.package,
        paySign: res.data.data.paySign,
        signType: res.data.data.signType,
        timeStamp: res.data.data.timeStamp
      })
    })
    this.setData({
      maskFlag: false,
      popupViewPay: false,

    })
  },
  //弹出支付请求
  showPaySuccess(e) {
    let _this = this
    wx.requestPayment({
      timeStamp: _this.data.timeStamp,
      nonceStr: _this.data.nonceStr,
      package: _this.data.packageInfo,
      signType: _this.data.signType,
      paySign: _this.data.paySign,
      success(res) {
        console.log(res, '支付成功')
        _this.setData({
          maskFlag: false,
          popupViewPay: true,
          popupViewEnroll: false
        })
      },
      fail(res) {
        wx.showToast({
          title: '支付出错',
          icon: 'none'
        })
        console.log(res, '支付失败')
      }
    })




  },
  goBackIndex(e) {
    wx.redirectTo({
      url: '/pages/myIndex/myIndex',
    })
  },
  closePopop(e) {
    // wx.redirectTo({
    //   url: '/pages/myIndex/myIndex',
    // })
    this.setData({
      maskFlag: true,
      popupViewPay: true,
      popupViewEnroll: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let money = wx.getStorageSync('money')
    this.setData({
      payPrice: money
    })
    if (wx.getStorageSync('pickerid')) {
      let areaIndex = wx.getStorageSync('pickerid')
      this.setData({
        areaChange: 1,
        areaIndex: areaIndex
      })
    }
    // 请求地区
    http.getHttp('/get_area', {}, {}).then(res => {
      console.log(res, '地区res')
      let areaArr = res.data.data
      areaArr.map(val => {
        val.tmp = `${val.province}-${val.city}-${val.district}`
      })
      this.setData({
        areaArray: areaArr,
        areaArr
      })
    })
    // 校区请求
    http.getHttp('/school_lists', {
      area: wx.getStorageSync('pickerid')
    }, {
      token: wx.getStorageSync('token')
    }).then(res => {
      console.log(res, '校区')
      if (res.data.data.length !== 0) {
        let schoolTmp = res.data.data
        let lat = ''
        let lng = ''
        schoolTmp.map(val => {
          lat = val.lat,
            lng = val.lng
        })
        this.setData({
          schoolArr: schoolTmp,
          lat,
          lng
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})